<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="640px" :title="`${dialogProps.title}会员`">
    <el-form
      style="height: 600px; overflow-y: auto"
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="会员头像" prop="avatarImage">
        <UploadImg v-model:image-url="dialogProps.row!.avatarImage" width="104px" height="104px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
          </template>
          <template #tip> 图片大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="会员名称" prop="userName">
        <el-input v-model="dialogProps.row!.userName" placeholder="请填会员名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="会员昵称" prop="nickName">
        <el-input v-model="dialogProps.row!.nickName" placeholder="请填会员昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dialogProps.row!.mobile" placeholder="请输入手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select class="w100" v-model="dialogProps.row!.sex">
          <el-option v-for="item in genderType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="会员等级" prop="grade">
        <el-select class="w100" v-model="dialogProps.row!.grade">
          <el-option v-for="item in memberGrade" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="会员分类" prop="userCategoryId">
        <el-select class="w100" v-model="dialogProps.row!.userCategoryId">
          <el-option v-for="item in memberCategory" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="会员卡号" prop="number">
        <el-input v-model="dialogProps.row!.number" placeholder="请输入会员卡号" clearable></el-input>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker v-model="dialogProps.row!.birthday" type="date" style="width: 100%" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select class="w100" v-model="dialogProps.row!.status" placeholder="请选择会员状态">
          <el-option v-for="item in memberStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="启用日期" prop="starDate">
        <el-date-picker style="width: 100%" v-model="dialogProps.row!.starDate" type="date" />
      </el-form-item>
      <el-form-item label="失效日期" prop="endDate">
        <el-date-picker style="width: 100%" v-model="dialogProps.row!.endDate" type="date" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="MemberDialog">
import { Member } from "@/api/interface";
import { getMemberGradeList, getUserCategoryList } from "@/api/modules/member";
import UploadImg from "@/components/Upload/Img.vue";
import { genderType, memberStatus } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { reactive, ref } from "vue";

const rules = reactive({
  userName: [{ required: true, message: "请填写会员名称" }],
  nickName: [{ required: true, message: "请填写会员昵称" }],
  mobile: [{ required: true, message: "请输入手机号" }],
  sex: [{ required: true, message: "请选择性别" }],
  // avatarImage: [{ required: true, message: "请上传头像" }],
  userCategoryId: [{ required: true, message: "请选择会员分类" }],
  grade: [{ required: true, message: "请选择会员等级" }],
  number: [{ required: true, message: "请输入会员卡号" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Member.ResMemberList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const memberGrade = ref([]);
const memberCategory = ref([]);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DialogProps) => {
  dialogProps.value = params;
  const [MemberGrade, MemberCategory] = await Promise.all([
    getMemberGradeList({ pageNum: 1, pageSize: 9999 }),
    getUserCategoryList({ ageNum: 1, pageSize: 9999 })
  ]);
  memberGrade.value = MemberGrade.data.list;
  memberCategory.value = MemberCategory.data.list;
  if (dialogProps.value.title === "新增") {
    dialogProps.value.row.grade = memberGrade.value.length !== 0 ? memberGrade.value[0].id : "";
    dialogProps.value.row.userCategoryId = memberCategory.value.length !== 0 ? memberCategory.value[0].id : "";
  }
  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.title}会员成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
